﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
	[Serializable()]
	public class DUPhieuYeuCauBAChiTietBO : Csla.BusinessBase<DUPhieuYeuCauBAChiTietBO>
	{
		#region Business Properties
		
private int? _DUPhieuYeuCauBAChiTietID = null;
public int? DUPhieuYeuCauBAChiTietID
{
get { return this._DUPhieuYeuCauBAChiTietID; }
set { this._DUPhieuYeuCauBAChiTietID = value; PropertyHasChanged("DUPhieuYeuCauBAChiTietID"); }
}

private int? _PhieuYeuCauBAID = null;
public int? PhieuYeuCauBAID
{
get { return this._PhieuYeuCauBAID; }
set { this._PhieuYeuCauBAID = value; PropertyHasChanged("PhieuYeuCauBAID"); }
}

private int? _ThuocIDNguon = null;
public int? ThuocIDNguon
{
get { return this._ThuocIDNguon; }
set { this._ThuocIDNguon = value; PropertyHasChanged("ThuocIDNguon"); }
}

private string _MaThuocNguon = null;
public string MaThuocNguon
{
get { return this._MaThuocNguon; }
set { this._MaThuocNguon = value; PropertyHasChanged("MaThuocNguon"); }
}

private string _TenThuocNguon = null;
public string TenThuocNguon
{
get { return this._TenThuocNguon; }
set { this._TenThuocNguon = value; PropertyHasChanged("TenThuocNguon"); }
}

private int? _DonViTinhIDNguon = null;
public int? DonViTinhIDNguon
{
get { return this._DonViTinhIDNguon; }
set { this._DonViTinhIDNguon = value; PropertyHasChanged("DonViTinhIDNguon"); }
}

private string _TenDVTNguon = null;
public string TenDVTNguon
{
get { return this._TenDVTNguon; }
set { this._TenDVTNguon = value; PropertyHasChanged("TenDVTNguon"); }
}

private int? _ThuocIDDich = null;
public int? ThuocIDDich
{
get { return this._ThuocIDDich; }
set { this._ThuocIDDich = value; PropertyHasChanged("ThuocIDDich"); }
}

private string _MaThuocDich = null;
public string MaThuocDich
{
get { return this._MaThuocDich; }
set { this._MaThuocDich = value; PropertyHasChanged("MaThuocDich"); }
}

private string _TenThuocDich = null;
public string TenThuocDich
{
get { return this._TenThuocDich; }
set { this._TenThuocDich = value; PropertyHasChanged("TenThuocDich"); }
}

private int? _DonViTinhIDDich = null;
public int? DonViTinhIDDich
{
get { return this._DonViTinhIDDich; }
set { this._DonViTinhIDDich = value; PropertyHasChanged("DonViTinhIDDich"); }
}

private string _TenDVTDich = null;
public string TenDVTDich
{
get { return this._TenDVTDich; }
set { this._TenDVTDich = value; PropertyHasChanged("TenDVTDich"); }
}

private DateTime? _NgayHetHan = null;
public DateTime? NgayHetHan
{
get { return this._NgayHetHan; }
set { this._NgayHetHan = value; PropertyHasChanged("NgayHetHan"); }
}

private double? _SoLuong = null;
public double? SoLuong
{
get { return this._SoLuong; }
set { this._SoLuong = value; PropertyHasChanged("SoLuong"); }
}

private double? _DonGia = null;
public double? DonGia
{
get { return this._DonGia; }
set { this._DonGia = value; PropertyHasChanged("DonGia"); }
}

private double? _ThanhTien = null;
public double? ThanhTien
{
get { return this._ThanhTien; }
set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
}

private int? _LuongDung = null;
public int? LuongDung
{
get { return this._LuongDung; }
set { this._LuongDung = value; PropertyHasChanged("LuongDung"); }
}

private int? _LieuLuongDung = null;
public int? LieuLuongDung
{
get { return this._LieuLuongDung; }
set { this._LieuLuongDung = value; PropertyHasChanged("LieuLuongDung"); }
}

private int? _LanDung = null;
public int? LanDung
{
get { return this._LanDung; }
set { this._LanDung = value; PropertyHasChanged("LanDung"); }
}

private int? _LieuLuongLan = null;
public int? LieuLuongLan
{
get { return this._LieuLuongLan; }
set { this._LieuLuongLan = value; PropertyHasChanged("LieuLuongLan"); }
}

private int? _KhoID = null;
public int? KhoID
{
get { return this._KhoID; }
set { this._KhoID = value; PropertyHasChanged("KhoID"); }
}

private string _GhiChu = null;
public string GhiChu
{
get { return this._GhiChu; }
set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
}

private int? _UserCreated = Session.LoginID;
public int? UserCreated
{
get { return this._UserCreated; }
set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
}

private DateTime? _DateCreated = DateTime.MaxValue;
public DateTime? DateCreated
{
get { return this._DateCreated; }
set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
}

private int? _UserModify = null;
public int? UserModify
{
get { return this._UserModify; }
set { this._UserModify = value; PropertyHasChanged("UserModify"); }
}

private DateTime? _DateModify = null;
public DateTime? DateModify
{
get { return this._DateModify; }
set { this._DateModify = value; PropertyHasChanged("DateModify"); }
}

private bool? _Deleted = false;
public bool? Deleted
{
get { return this._Deleted; }
set { this._Deleted = value; PropertyHasChanged("Deleted"); }
}

		private int? _STT0 = 0;
		public int? STT0
		{
			get { return _STT0; }
			set { _STT0 = value; }
		}
		
		protected override object GetIdValue()
		{
			return _DUPhieuYeuCauBAChiTietID.Value;
		}
		#endregion Business Properties
		//==========================================================================
		
		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}
		
		public void SetIsDirty()
        {
            MarkDirty();
        }

        public void SetIsChild()
        {
            MarkAsChild();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }
		
		private static int newDUPhieuYeuCauBAChiTietID =  -1;
private DUPhieuYeuCauBAChiTietBO()
{
_DUPhieuYeuCauBAChiTietID = newDUPhieuYeuCauBAChiTietID--;
}

		public DUPhieuYeuCauBAChiTietBO(SqlDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public DUPhieuYeuCauBAChiTietBO(SqlDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
                MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static DUPhieuYeuCauBAChiTietBO NewRootDUPhieuYeuCauBAChiTietBO()
		{
			return DataPortal.Create<DUPhieuYeuCauBAChiTietBO>(new RootCriteria());
		}

		internal static DUPhieuYeuCauBAChiTietBO NewChildDUPhieuYeuCauBAChiTietBO()
		{
			return DataPortal.Create<DUPhieuYeuCauBAChiTietBO>(new ChildCriteria());
		}

		public static DUPhieuYeuCauBAChiTietBO GetRootDUPhieuYeuCauBAChiTietBO(int _DUPhieuYeuCauBAChiTietID)
		{
			return DataPortal.Fetch<DUPhieuYeuCauBAChiTietBO>(new RootCriteria(_DUPhieuYeuCauBAChiTietID));
		}

		public static DUPhieuYeuCauBAChiTietBO GetRootDUPhieuYeuCauBAChiTietBOByWhere(string where)
		{
			return DataPortal.Fetch<DUPhieuYeuCauBAChiTietBO>(where);
		}

		internal static DUPhieuYeuCauBAChiTietBO GetChildDUPhieuYeuCauBAChiTietBO(SqlDataReader dataReader)
		{
			return new DUPhieuYeuCauBAChiTietBO(dataReader);
		}

		public static void DeleteDUPhieuYeuCauBAChiTietBO(int _DUPhieuYeuCauBAChiTietID)
		{
			DataPortal.Delete(new RootCriteria(_DUPhieuYeuCauBAChiTietID));
		}

		public static void DeleteDUPhieuYeuCauBAChiTietBO(int _DUPhieuYeuCauBAChiTietID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(_DUPhieuYeuCauBAChiTietID, directDelete));
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int DUPhieuYeuCauBAChiTietID;
			public bool directDelete = false;
			public RootCriteria(int _DUPhieuYeuCauBAChiTietID)
			{
				this.DUPhieuYeuCauBAChiTietID = _DUPhieuYeuCauBAChiTietID;
				this.directDelete = false;
			}
			
			public RootCriteria(int _DUPhieuYeuCauBAChiTietID, bool directDelete)
            {
                this.DUPhieuYeuCauBAChiTietID = _DUPhieuYeuCauBAChiTietID;
                this.directDelete = directDelete;
            }
            
			public RootCriteria() {}
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int DUPhieuYeuCauBAChiTietID;
			public bool directDelete = false;
			public ChildCriteria(int _DUPhieuYeuCauBAChiTietID)
			{
				this.DUPhieuYeuCauBAChiTietID = _DUPhieuYeuCauBAChiTietID;
				this.directDelete = false;
			}
			
			public ChildCriteria(int _DUPhieuYeuCauBAChiTietID, bool directDelete)
            {
                this.DUPhieuYeuCauBAChiTietID = _DUPhieuYeuCauBAChiTietID;
                this.directDelete = directDelete;
            }
            
			public ChildCriteria() {}
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}
		
		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuYeuCauBAChiTietBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[DUPhieuYeuCauBAChiTietID] = " + criteria.DUPhieuYeuCauBAChiTietID);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void This_Fetch(string where)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuYeuCauBAChiTietBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void Fetch(SqlDataReader dataReader)
		{
			if (!(dataReader["DUPhieuYeuCauBAChiTietID"] is DBNull))
this._DUPhieuYeuCauBAChiTietID = (int)dataReader["DUPhieuYeuCauBAChiTietID"];
if (!(dataReader["PhieuYeuCauBAID"] is DBNull))
this._PhieuYeuCauBAID = (int)dataReader["PhieuYeuCauBAID"];
if (!(dataReader["ThuocIDNguon"] is DBNull))
this._ThuocIDNguon = (int)dataReader["ThuocIDNguon"];
if (!(dataReader["MaThuocNguon"] is DBNull))
this._MaThuocNguon = (string)dataReader["MaThuocNguon"];
if (!(dataReader["TenThuocNguon"] is DBNull))
this._TenThuocNguon = (string)dataReader["TenThuocNguon"];
if (!(dataReader["DonViTinhIDNguon"] is DBNull))
this._DonViTinhIDNguon = (int)dataReader["DonViTinhIDNguon"];
if (!(dataReader["TenDVTNguon"] is DBNull))
this._TenDVTNguon = (string)dataReader["TenDVTNguon"];
if (!(dataReader["ThuocIDDich"] is DBNull))
this._ThuocIDDich = (int)dataReader["ThuocIDDich"];
if (!(dataReader["MaThuocDich"] is DBNull))
this._MaThuocDich = (string)dataReader["MaThuocDich"];
if (!(dataReader["TenThuocDich"] is DBNull))
this._TenThuocDich = (string)dataReader["TenThuocDich"];
if (!(dataReader["DonViTinhIDDich"] is DBNull))
this._DonViTinhIDDich = (int)dataReader["DonViTinhIDDich"];
if (!(dataReader["TenDVTDich"] is DBNull))
this._TenDVTDich = (string)dataReader["TenDVTDich"];
if (!(dataReader["NgayHetHan"] is DBNull))
this._NgayHetHan = (DateTime)dataReader["NgayHetHan"];
if (!(dataReader["SoLuong"] is DBNull))
this._SoLuong = (double)dataReader["SoLuong"];
if (!(dataReader["DonGia"] is DBNull))
this._DonGia = (double)dataReader["DonGia"];
if (!(dataReader["ThanhTien"] is DBNull))
this._ThanhTien = (double)dataReader["ThanhTien"];
if (!(dataReader["LuongDung"] is DBNull))
this._LuongDung = (int)dataReader["LuongDung"];
if (!(dataReader["LieuLuongDung"] is DBNull))
this._LieuLuongDung = (int)dataReader["LieuLuongDung"];
if (!(dataReader["LanDung"] is DBNull))
this._LanDung = (int)dataReader["LanDung"];
if (!(dataReader["LieuLuongLan"] is DBNull))
this._LieuLuongLan = (int)dataReader["LieuLuongLan"];
if (!(dataReader["KhoID"] is DBNull))
this._KhoID = (int)dataReader["KhoID"];
if (!(dataReader["GhiChu"] is DBNull))
this._GhiChu = (string)dataReader["GhiChu"];
if (!(dataReader["UserCreated"] is DBNull))
this._UserCreated = (int)dataReader["UserCreated"];
if (!(dataReader["DateCreated"] is DBNull))
this._DateCreated = (DateTime)dataReader["DateCreated"];
if (!(dataReader["UserModify"] is DBNull))
this._UserModify = (int)dataReader["UserModify"];
if (!(dataReader["DateModify"] is DBNull))
this._DateModify = (DateTime)dataReader["DateModify"];
if (!(dataReader["Deleted"] is DBNull))
this._Deleted = (bool)dataReader["Deleted"];

		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			this._UserCreated = Session.LoginID;
this._DateCreated = DateTime.MaxValue;
this._UserModify = null;
this._DateModify = null;
this._Deleted = false;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuYeuCauBAChiTietBO_InsertBO");
			db.AddInParameter(cmd, "@PhieuYeuCauBAID", SqlDbType.Int, this._PhieuYeuCauBAID);
db.AddInParameter(cmd, "@ThuocIDNguon", SqlDbType.Int, this._ThuocIDNguon);
db.AddInParameter(cmd, "@MaThuocNguon", SqlDbType.NVarChar, this._MaThuocNguon);
db.AddInParameter(cmd, "@TenThuocNguon", SqlDbType.NVarChar, this._TenThuocNguon);
db.AddInParameter(cmd, "@DonViTinhIDNguon", SqlDbType.Int, this._DonViTinhIDNguon);
db.AddInParameter(cmd, "@TenDVTNguon", SqlDbType.NVarChar, this._TenDVTNguon);
db.AddInParameter(cmd, "@ThuocIDDich", SqlDbType.Int, this._ThuocIDDich);
db.AddInParameter(cmd, "@MaThuocDich", SqlDbType.NVarChar, this._MaThuocDich);
db.AddInParameter(cmd, "@TenThuocDich", SqlDbType.NVarChar, this._TenThuocDich);
db.AddInParameter(cmd, "@DonViTinhIDDich", SqlDbType.Int, this._DonViTinhIDDich);
db.AddInParameter(cmd, "@TenDVTDich", SqlDbType.NVarChar, this._TenDVTDich);
db.AddInParameter(cmd, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
db.AddInParameter(cmd, "@SoLuong", SqlDbType.Float, this._SoLuong);
db.AddInParameter(cmd, "@DonGia", SqlDbType.Float, this._DonGia);
db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
db.AddInParameter(cmd, "@LuongDung", SqlDbType.Int, this._LuongDung);
db.AddInParameter(cmd, "@LieuLuongDung", SqlDbType.Int, this._LieuLuongDung);
db.AddInParameter(cmd, "@LanDung", SqlDbType.Int, this._LanDung);
db.AddInParameter(cmd, "@LieuLuongLan", SqlDbType.Int, this._LieuLuongLan);
db.AddInParameter(cmd, "@KhoID", SqlDbType.Int, this._KhoID);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
db.AddOutParameter(cmd, "@NewDUPhieuYeuCauBAChiTietID", SqlDbType.Int, 10);
db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			this._DUPhieuYeuCauBAChiTietID = (int)cmd.Parameters["@NewDUPhieuYeuCauBAChiTietID"].Value;
this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			this._UserModify = Session.LoginID;
this._DateModify = DateTime.Now;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuYeuCauBAChiTietBO_UpdateBO");
			db.AddInParameter(cmd, "@DUPhieuYeuCauBAChiTietID", SqlDbType.Int, this._DUPhieuYeuCauBAChiTietID);
db.AddInParameter(cmd, "@PhieuYeuCauBAID", SqlDbType.Int, this._PhieuYeuCauBAID);
db.AddInParameter(cmd, "@ThuocIDNguon", SqlDbType.Int, this._ThuocIDNguon);
db.AddInParameter(cmd, "@MaThuocNguon", SqlDbType.NVarChar, this._MaThuocNguon);
db.AddInParameter(cmd, "@TenThuocNguon", SqlDbType.NVarChar, this._TenThuocNguon);
db.AddInParameter(cmd, "@DonViTinhIDNguon", SqlDbType.Int, this._DonViTinhIDNguon);
db.AddInParameter(cmd, "@TenDVTNguon", SqlDbType.NVarChar, this._TenDVTNguon);
db.AddInParameter(cmd, "@ThuocIDDich", SqlDbType.Int, this._ThuocIDDich);
db.AddInParameter(cmd, "@MaThuocDich", SqlDbType.NVarChar, this._MaThuocDich);
db.AddInParameter(cmd, "@TenThuocDich", SqlDbType.NVarChar, this._TenThuocDich);
db.AddInParameter(cmd, "@DonViTinhIDDich", SqlDbType.Int, this._DonViTinhIDDich);
db.AddInParameter(cmd, "@TenDVTDich", SqlDbType.NVarChar, this._TenDVTDich);
db.AddInParameter(cmd, "@NgayHetHan", SqlDbType.DateTime, this._NgayHetHan);
db.AddInParameter(cmd, "@SoLuong", SqlDbType.Float, this._SoLuong);
db.AddInParameter(cmd, "@DonGia", SqlDbType.Float, this._DonGia);
db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
db.AddInParameter(cmd, "@LuongDung", SqlDbType.Int, this._LuongDung);
db.AddInParameter(cmd, "@LieuLuongDung", SqlDbType.Int, this._LieuLuongDung);
db.AddInParameter(cmd, "@LanDung", SqlDbType.Int, this._LanDung);
db.AddInParameter(cmd, "@LieuLuongLan", SqlDbType.Int, this._LieuLuongLan);
db.AddInParameter(cmd, "@KhoID", SqlDbType.Int, this._KhoID);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			
this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(_DUPhieuYeuCauBAChiTietID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuYeuCauBAChiTietBO_DeleteBO");
			db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
			db.AddInParameter(cmd, "@DUPhieuYeuCauBAChiTietID", SqlDbType.Int, criteria.DUPhieuYeuCauBAChiTietID);
			db.ExecuteNonQuery(cmd);
		}
		#endregion Delete
		//==========================================================================

		#region Internal method
		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(_DUPhieuYeuCauBAChiTietID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}
